package edu.hitsz.application;

import edu.hitsz.rankinglist.RankDaoImpl;

import javax.swing.*;
import java.awt.*;

/**
 * 程序入口
 * @author hitsz
 */
public class Main {

    public static final int WINDOW_WIDTH = 530;
    public static final int WINDOW_HEIGHT = 768;

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Hello Aircraft War");

        // 获得屏幕的分辨率，初始化 Frame
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame = new JFrame("Aircraft War");
        frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setResizable(false);
        //设置窗口的大小和位置,居中放置
        frame.setBounds(((int) screenSize.getWidth() - WINDOW_WIDTH) / 2, 0,
                WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ModeSelect mode = new ModeSelect();
        frame.setContentPane(mode.getPanel());
        frame.setVisible(true);

        // 暂停主线程
        Object lock = mode.getLock();
        synchronized (mode.getLock()){
            lock.wait();
        }
        Game game;
        // 等待难度选择界面唤醒
        if(mode.getModeType() == 1){
            game = new EasyGame(mode.getMusicSwitch(),mode.getUser());
        }
        else if(mode.getModeType() == 2){
            game = new NormalGame(mode.getMusicSwitch(),mode.getUser());
        }
        else{
            game = new HardGame(mode.getMusicSwitch(),mode.getUser());
        }

        frame.setContentPane(game);
        frame.setVisible(true);

        game.action();

        // 暂停主进程
        synchronized (game) {
            game.wait();
        }

        // 排行榜页面 等待game进程唤醒
        RankDaoImpl ranks = game.getRanks();
        RankList rankList = new RankList(ranks,mode.getModeType(),game.getScore());
        frame.setContentPane(rankList.getPanel());
        frame.setVisible(true);


    }
}
